package com.example.demo.services;

import com.example.demo.entity.Product.*;
import com.example.demo.entity.ProductDTO;
import com.example.demo.repository.*;
import jakarta.persistence.EntityManager;
import jakarta.persistence.ParameterMode;
import jakarta.persistence.StoredProcedureQuery;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ProduceWpInRepairService {

    private final EntityManager entityManager;

    private final ProduceWpInRepairRepository repo;
    private final FilesRepository filesRepo;
    private final UnitCNRepository unitCNRepo;
    private final WorkAreaCNRepository workAreaCNRepo;
    private final WorkProcedureRepository workProcedureRepo;
    private final QtyRepository qtyRepo;

    public ProduceWpInRepairService(ProduceWpInRepairRepository repo, FilesRepository filesRepo, UnitCNRepository unitCNRepo, WorkAreaCNRepository workAreaCNRepo, WorkProcedureRepository workProcedureRepo, QtyRepository qtyRepo, EntityManager entityManager, ProduceWpInRepairRepository produceWpInRepairRepository, ProductDetailRepository productDetailRepository) {
        this.repo = repo;
        this.filesRepo = filesRepo;
        this.unitCNRepo = unitCNRepo;
        this.workAreaCNRepo = workAreaCNRepo;
        this.workProcedureRepo = workProcedureRepo;
        this.qtyRepo = qtyRepo;
        this.entityManager = entityManager;
    }

    public ProductDTO findByNo(String billNo) {
        Product product = repo.findByNo(billNo);
        if (product == null) {
            return null;
        }
        /*单位转中文*/
        String unitCode = product.getUnitCode();
        Units units = unitCNRepo.getUnitName(unitCode);
        if (units != null) {
            String unitCN = units.getUnit();
            product.setUnitCode(unitCN);
        }
        /*指定仓库转中文*/
        String areaCode = product.getWorkingAreaCode();
        WorkArea area = workAreaCNRepo.getAreaName(areaCode);
        if (area != null) {
            String areaCN = area.getWorkingAreaName();
            product.setWorkingAreaCode(areaCN);
        }
        /*工序转中文*/
        String wpCode = product.getWpCode();
        WorkProcedure wp = workProcedureRepo.getName(wpCode);
        if (wp != null) {
            String wpCN = wp.getWpName();
            product.setWpCode(wpCN);
        }
        /*获取可进仓数量*/
        Qty qty = qtyRepo.getQty(billNo,wpCode);
        if (qty != null) {
            double canInQty = 0.0;
            if (qty.getParmMustCompleteWork() == 'y' ) {
                canInQty = qty.getGoodQty() - qty.getInQty();
            }else {
                canInQty = qty.getPlanQty() - qty.getInQty();
            }
            product.setQty(canInQty);
        }
        /*获取图片地址*/
        String goodsCode = product.getGoodsCode();
        Files files = filesRepo.findImg(goodsCode);
        FilesDTO filesDTO = new FilesDTO();
        if (files != null) {
            filesDTO.setImageUrl(files.getFilePathname());
            filesDTO.setPicFormat(files.getPicFormat());
        }
        /*获取pdf地址*/
        files = filesRepo.findPDF(goodsCode);
        if (files != null) {
            filesDTO.setPdfUrl(files.getFilePathname());
        }
        return new ProductDTO(product, filesDTO);
    }

    public int updateStatus(String billNo, String userId, String userName, String modeID) {
        LocalDateTime now = LocalDateTime.now();
        int num = 0;
        String status = repo.getStatus(billNo);
        if(Objects.equals(status, "已审核") || Objects.equals(status, "已生成采购订单")) {
            num = 1;
        }else if(Objects.equals(status, "未审核") && modeID.equals("审核")) {
            StoredProcedureQuery query = entityManager.createStoredProcedureQuery("P_WP_produce_in_bill_checked");
            query.registerStoredProcedureParameter("bill_no", String.class, ParameterMode.IN);
            query.registerStoredProcedureParameter("g_user_id", String.class, ParameterMode.IN);
            query.registerStoredProcedureParameter("g_user_name", String.class, ParameterMode.IN);
            query.registerStoredProcedureParameter("check_date", LocalDateTime.class, ParameterMode.IN);
            query.registerStoredProcedureParameter("ok", Long.class, ParameterMode.OUT);  //输出参数

            query.setParameter("bill_no", billNo);
            query.setParameter("g_user_id", userId);
            query.setParameter("g_user_name", userName);
            query.setParameter("check_date", now);

            Long result = (Long) query.getOutputParameterValue("ok");
            //System.out.println(result);
            query.execute();

            if (result == 1) {
                num = 2;
            }
        }
        //1 审核失败 -- 已审核不可再审
        //2 审核成功
        //0 审核失败 -- 错误
        return num;
    }

    public List<WorkArea> getAllWorkArea(String property) {
        return workAreaCNRepo.getAllWorkArea(property);
    }

    public List<WorkArea> getUserWorkArea(String property, String userId) {
        return workAreaCNRepo.getUserWorkArea(property, userId);
    }
}
